﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Windows;
using Fluent;
using PefStudio.Base;
using PefStudio.StartUp;
using PefStudio.Windows;

namespace PefStudio.Windows
{
    public class AbstractRibbonTabItem : RibbonTabItem, IRibbonTabItem, IPartImportsSatisfiedNotification
    {
        [Import(Bootstrapper.MainWindowContract, typeof(Window))]
        private Lazy<MainWindow> mainWindow { get; set; }
        protected MainWindow MainWindow
        {
            get { return mainWindow.Value; }
        }

        public string ID { get; set; }
        public double Index { get; set; }
        public string InsertRelativeToID { get; set; }
        public bool BeforeRelativeID { get; set; }

        protected string BaseContract { get; set; }
        public virtual void OnImportsSatisfied()
        {
            try
            {
                var groupBoxs =
                    MainWindow.CompositionContainer.GetExportedValues<IRibbonGroupBox>(
                        BaseContract);
                CreateService.CreateCollection(Groups, groupBoxs);
            }
            catch (ImportCardinalityMismatchException)
            {
            }
            try
            {
                var contextualGroup =
                    MainWindow.CompositionContainer.GetExportedValue<IRibbonContextualTabGroup>(
                        BaseContract);
                Group = contextualGroup as RibbonContextualTabGroup;
            }
            catch (ImportCardinalityMismatchException)
            {
            }
        }
    }

    public class AbstractRibbonGroupBox : RibbonGroupBox, IRibbonGroupBox, IPartImportsSatisfiedNotification
    {
        [Import(Bootstrapper.MainWindowContract, typeof(Window))]
        private Lazy<MainWindow> mainWindow { get; set; }
        protected MainWindow MainWindow
        {
            get { return mainWindow.Value; }
        }

        private string _id;
        public string ID
        {
            get { return _id; }
            set
            {
                _id = value;
                Name = _id;
            }
        }
        public double Index { get; set; }
        public string InsertRelativeToID { get; set; }
        public bool BeforeRelativeID { get; set; }

        protected string BaseContract { get; set; }
        public virtual void OnImportsSatisfied()
        {
            try
            {
                var childItems =
                   MainWindow.CompositionContainer.GetExportedValues<IRibbonChildItem>(
                       BaseContract);
                CreateService.CreateCollection(Items, childItems);
            }
            catch (ImportCardinalityMismatchException)
            {
            }
            try
            {
                var toolBars =
                   MainWindow.CompositionContainer.GetExportedValues<IRibbonToolBar>(
                       BaseContract);
                CreateService.CreateCollection(Items, toolBars);
            }
            catch (ImportCardinalityMismatchException)
            {
            }
        }
    }

    public class AbstractRibbonContextualTabGroup : RibbonContextualTabGroup, IRibbonContextualTabGroup, IPartImportsSatisfiedNotification
    {
        [Import(Bootstrapper.MainWindowContract, typeof(Window))]
        private Lazy<MainWindow> mainWindow { get; set; }
        protected MainWindow MainWindow
        {
            get { return mainWindow.Value; }
        }

        public string ID { get; set; }
        public double Index { get; set; }
        public string InsertRelativeToID { get; set; }
        public bool BeforeRelativeID { get; set; }

        protected string BaseContract { get; set; }
        public virtual void OnImportsSatisfied()
        {
            try
            {
                var tabItems =
                   MainWindow.CompositionContainer.GetExportedValues<IRibbonTabItem>(
                       BaseContract);
                foreach(RibbonTabItem item in tabItems)
                {
                    item.Group = this;
                }
            }
            catch (ImportCardinalityMismatchException)
            {
            }
        }
    }
}
